package com.link.base.base.approval.service;

import com.link.base.base.approval.dao.mybatis.mapper.FlowUserMapper;
import com.link.base.base.approval.model.FlowUser;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @date: 2018/11/8 15:07
 * @version 1.0
 */

@Service
public class FlowUserServiceImpl extends BasicServiceImpl<FlowUser> implements FlowUserService {

    @Resource
    private FlowUserMapper flowUserMapper;

    @Resource
    private KeyGenerateService keyGenerateService;

    /**
     * 新增数据
     * @param userId
     * @param flowId
     * @param operate
     * @throws Exception
     */
    @Override
    public void insert(Long userId, Long flowId, String operate) throws Exception {
        if (userId == null) {
            throw new ServiceException("User" + "PUBLIC-007");
        }
        if (flowId == null) {
            throw new ServiceException("flow" + "PUBLIC-007");
        }
        FlowUser record = new FlowUser();
        record.setId(keyGenerateService.keyGenerate());
        record.setUserId(userId);
        record.setFlowId(flowId);
        record.setOperate(operate);
        flowUserMapper.insert(record);
    }

    /**
     * 查询列表
     * @param flowUser
     * @return List
     * @throws Exception
     */
    @Override
    public List<FlowUser> queryByFLowId(FlowUser flowUser) throws Exception {
        Long flowId = -1L;
        QueryParams queryParams = flowUser.getQps();
        if (queryParams != null) {
            List<Filter> filterList = queryParams.getFilters();

            if (filterList != null && filterList.size() > 0) {
                for (Filter filter : filterList) {
                    if ("flowId".equals(filter.getProperty())) {
                        String value = filter.getValue();
                        flowId = Long.valueOf(value);
                    }
                }
            }
        } else {
            flowId = flowUser.getFlowId();
        }
        if (flowId == null || flowId == -1L) {
            throw new ServiceException("FLOWPUBLIC-007");
        }
        return flowUserMapper.queryByFLowId(flowId);
    }

    public List<FlowUser> queryByFLowId(Long flowId) throws Exception {
        if (flowId == null || flowId == -1L) {
            throw new ServiceException("FLOWPUBLIC-007");
        }
        return flowUserMapper.queryByFLowId(flowId);
    }

    /**
     * 根据审批流，删除待审批人信息
     * @param flowId
     * @throws Exception
     */
    @Override
    public void deleteByFlowId(Long flowId) throws Exception {
        if (flowId == null) {
            throw new ServiceException("flow" + "PUBLIC-007");
        }
        FlowUser flowUser = new FlowUser();
        flowUser.setFlowId(flowId);
        flowUserMapper.deleteByFlowId(flowUser);
    }

    /**
     * 更新状态
     * @param id
     * @param operate
     * @throws Exception
     */
    @Override
    public void update(Long id, String operate) throws Exception {
        if (id == null) {
            throw new ServiceException("PUBLIC-007");
        }
        if (operate == null || "".equals(operate)) {
            throw new ServiceException("APPROVAL-034");
        }
        FlowUser record = new FlowUser();
        record.setId(id);
        record.setOperate(operate);
        flowUserMapper.update(record);
    }

    /**
     * 更新已读标记
     * @param entity
     * @throws Exception
     */
    @Override
    public void readFlagUpdate(FlowUser entity) throws Exception {
        if (null == entity.getFlowId()) {
            throw new ServiceException("流程" + "PUBLIC-007");
        }
        if (null == entity.getUserId()) {
            throw new ServiceException("PUBLIC-016");
        }
        flowUserMapper.readFlagUpdate(entity);
    }
}
